package com.classroom.smart.controller;

import com.classroom.smart.constant.MessageConstant;
import com.classroom.smart.pojo.Manager;
import com.classroom.smart.pojo.Teacher;
import com.classroom.smart.pojo.User;
import com.classroom.smart.result.CommonResult;
import com.classroom.smart.result.Result;
import com.classroom.smart.service.ManagerService;
import com.classroom.smart.service.TeacherService;
import com.classroom.smart.service.UserService;
import com.classroom.smart.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class IndexController {

    @Autowired(required = false)
    private UserService userService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private ManagerService managerService;

    //处理登录请求的接口
    @PostMapping("login")
    @ResponseBody
    public CommonResult login(@RequestBody LoginVO vo) {
        if (StringUtils.hasLength(vo.getUsername()) && StringUtils.hasLength(vo.getPassword())) {
            if (vo.getPerson() == 1){
                //学生
                User user = userService.login(vo);
                return new CommonResult(200,"登陆成功",user);
            }else if (vo.getPerson() == 2){
                //老师
                Teacher teacher = teacherService.login(vo);
                return new CommonResult(200,"登陆成功",teacher);
            }else {
                //管理员
                Manager manager = managerService.login(vo);
                return new CommonResult(200,"登陆成功",manager);
            }
        }
        //失败信息
        return new CommonResult(455,"登陆失败");

    }

}
